package com.ccjk.beusoft.hot;

import android.content.ClipData;
import android.os.IBinder;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created by CCJK on 2017/12/5.
 */

public class ClipboardHookHandler implements InvocationHandler {
    Object origin;

    public ClipboardHookHandler(IBinder binder, Class<?> clazz) {
        try {
            Method asInterfaceMethod = clazz.getDeclaredMethod("asInterface", new Class[]{IBinder.class});

            //将binder对象转换成IClipboard接口
            this.origin = asInterfaceMethod.invoke(null, binder);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        // 把剪切版的内容替换为 "you are hooked"
        if ("getPrimaryClip".equals(method.getName())) {
            return ClipData.newPlainText(null, "you are hooked");
        }

        // 欺骗系统,使之认为剪切版上一直有内容
        if ("hasPrimaryClip".equals(method.getName())) {
            return true;
        }
        return method.invoke(origin, args);
    }
}
